//
//  HMStatus.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/13.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

class HMStatus: NSObject {

    var created_at: String?
    
    
    /// 微博ID
    var id:	Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String? {
        didSet{
        
            //当KVC来源赋值,我们重新处理来源
            
            if let newSource = source {
            
            //截取字符串
                guard let firstRange = newSource.rangeOfString("\">") else {
                HMPrint("没有匹配")
                return
                }
                
                guard let second = newSource.rangeOfString("</a>") else {

                    return
            }
                
                let range = firstRange.endIndex..<second.startIndex
                
                source = "来自" + newSource.substringWithRange(range)
        
        }
        }}
    
    /// 微博的配图 [Int], [String: AnyObject], [[String: AnyObject]]
    /// 数组,里面保存的是字典
    var pic_urls:[[String: AnyObject]]?{
        didSet{
        let count = pic_urls?.count ?? 0
            
            if count == 0 {
                return
            }
            
            storePicutreURLs = [NSURL]()
            
            for dict in pic_urls!{
            
            let urlString = dict["thumbnail_pic"]as! String
                let url = NSURL(string: urlString)!
                
                storePicutreURLs?.append(url)
            
            }
        
        
        }
    
    
    }
    
    var storePicutreURLs: [NSURL]?

    
    var rowHeight: CGFloat?
    
    var user: HMUser?
    //字典转模型
    
    var retweeted_status:HMStatus?
    
    //拦截模型,当user的时候,拦截,转模型,然后返回
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //拦截
        if key == "user" {
            //判断value是不是字典
            if let dict = value as? [String: AnyObject]{
            
            user = HMUser(dict: dict)
            }
            return
        }
        
        //拦截转发字典
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject]{
                
                retweeted_status = HMStatus(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    //对象的打印
    
    override var description: String{
        get {
        let key = ["created_at", "id", "text", "source", "pic_urls","user","retweeted_status"]
            
            return self.dictionaryWithValuesForKeys(key).description}}
        
}
